package com.zingrow.common.utils;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NetworkUtil {
    /**
     * Logger for this class
     */
    private static Logger logger = LoggerFactory.getLogger(NetworkUtil.class);

    /**
     * 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址;
     * 
     * @param request
     * @return
     * @throws IOException
     */
    public final static String getIpAddress(HttpServletRequest request) throws IOException {
        // 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址

        String ip = request.getHeader("X-Forwarded-For");
        if (logger.isInfoEnabled()) {
            logger.info("getIpAddress(HttpServletRequest) - X-Forwarded-For - String ip=" + ip);
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
                if (logger.isInfoEnabled()) {
                    logger.info("getIpAddress(HttpServletRequest) - Proxy-Client-IP - String ip=" + ip);
                }
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
                if (logger.isInfoEnabled()) {
                    logger.info("getIpAddress(HttpServletRequest) - WL-Proxy-Client-IP - String ip=" + ip);
                }
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
                if (logger.isInfoEnabled()) {
                    logger.info("getIpAddress(HttpServletRequest) - HTTP_CLIENT_IP - String ip=" + ip);
                }
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
                if (logger.isInfoEnabled()) {
                    logger.info("getIpAddress(HttpServletRequest) - HTTP_X_FORWARDED_FOR - String ip=" + ip);
                }
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
                if (logger.isInfoEnabled()) {
                    logger.info("getIpAddress(HttpServletRequest) - getRemoteAddr - String ip=" + ip);
                }
            }
        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = (String) ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }
    /** 
     * 获取操作系统,浏览器及浏览器版本信息 
     * @param request 
     * @return 
     */  
    public static String getOsAndBrowserInfo(HttpServletRequest request){  
        String  browserDetails  =   request.getHeader("User-Agent");  
        String  userAgent       =   browserDetails;  
        String  user            =   userAgent.toLowerCase();  
  
        String os = "";  
        String browser = "";  
  
        //=================OS Info=======================  
        if (userAgent.toLowerCase().indexOf("windows") >= 0 )  
        {  
            os = "Windows";   
        } else if(userAgent.toLowerCase().indexOf("mac") >= 0)  
        {  
            os = "Mac";  
        } else if(userAgent.toLowerCase().indexOf("x11") >= 0)  
        {  
            os = "Unix";  
        } else if(userAgent.toLowerCase().indexOf("android") >= 0)  
        {  
            os = "Android";  
        } else if(userAgent.toLowerCase().indexOf("iphone") >= 0)  
        {  
            os = "IPhone";  
        }else{  
            os = "UnKnown, More-Info: "+userAgent;  
        }  
        //===============Browser===========================  
        if (user.contains("edge"))  
        {  
            browser=(userAgent.substring(userAgent.indexOf("Edge")).split(" ")[0]).replace("/", "-");  
        } else if (user.contains("msie"))  
        {  
            String substring=userAgent.substring(userAgent.indexOf("MSIE")).split(";")[0];  
            browser=substring.split(" ")[0].replace("MSIE", "IE")+"-"+substring.split(" ")[1];  
        } else if (user.contains("safari") && user.contains("version"))  
        {  
            browser=(userAgent.substring(userAgent.indexOf("Safari")).split(" ")[0]).split("/")[0]  
                    + "-" +(userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];  
        } else if ( user.contains("opr") || user.contains("opera"))  
        {  
            if(user.contains("opera")){  
                browser=(userAgent.substring(userAgent.indexOf("Opera")).split(" ")[0]).split("/")[0]  
                        +"-"+(userAgent.substring(userAgent.indexOf("Version")).split(" ")[0]).split("/")[1];  
            }else if(user.contains("opr")){  
                browser=((userAgent.substring(userAgent.indexOf("OPR")).split(" ")[0]).replace("/", "-"))  
                        .replace("OPR", "Opera");  
            }  
  
        } else if (user.contains("chrome"))  
        {  
            if(user.contains("qqbrowser"))
            {
            browser=(userAgent.substring(userAgent.indexOf("QQBrowser")).split(" ")[0]).replace("/", "-");
            }else
            {browser=(userAgent.substring(userAgent.indexOf("Chrome")).split(" ")[0]).replace("/", "-");  
            }
            
        } else if ((user.indexOf("mozilla/7.0") > -1) || (user.indexOf("netscape6") != -1)  ||  
                (user.indexOf("mozilla/4.7") != -1) || (user.indexOf("mozilla/4.78") != -1) ||  
                (user.indexOf("mozilla/4.08") != -1) || (user.indexOf("mozilla/3") != -1) )  
        {  
            browser = "Netscape-?";  
  
        } else if (user.contains("firefox"))  
        {  
            browser=(userAgent.substring(userAgent.indexOf("Firefox")).split(" ")[0]).replace("/", "-");  
        } else if(user.contains("rv"))  
        {  
            String IEVersion = (userAgent.substring(userAgent.indexOf("rv")).split(" ")[0]).replace("rv:", "-");  
            browser="IE" + IEVersion.substring(0,IEVersion.length() - 1);  
        } 
        else
        {  
            browser = "UnKnown, More-Info: "+userAgent;  
        }  
  
        return os +" --- "+ browser ;  
    }  
}